package io.geekidea.springbootplus.system.schedule;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.system.constant.TaskStatusConstant;
import io.geekidea.springbootplus.system.entity.Task;
import io.geekidea.springbootplus.system.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class TaskScheduled {
    @Autowired
    TaskService taskService;

    @Scheduled(cron = "0 0 1 * * ?")
    public void hello() throws Exception {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(new Date());

        Date date = sdf.parse(str);

        List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().lt(Task::getSubmitTime,date).eq(Task::getStatus, TaskStatusConstant.UNTAKE));

        Date newSubmitTime = new Date();

        list.forEach(item->{
            item.setSubmitTime(newSubmitTime);
        });

        taskService.saveOrUpdateBatch(list);
    }
}
